/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
/*#include "main.h"*/
#include "celpfilt.h"
#include "movarray.h"
#include "setarray.h"

/* LP Analysis Filter Parameters */
FILTER	InputHPFZ, InputHPFP;
float 	InputHPFCoefsZ[3] = {0.946, -1.892, 0.946};
float 	InputHPFCoefsP[3] = {1.0, -1.889033, 0.8948743};
int	InputHPFOrder = 2;
int	InputHPFLength = F_LEN;

/* LP residual calculation filter parameters */
FILTER	LP_ResZ, LP_ResP, LP_ResP2;

/* Adaptive analysis residual calculation filter parameters */
FILTER	Adapt_ResZ, Adapt_ResP, Adapt_ResP2;

/* Update filters for residual calculations */
FILTER	Update_ResZ, Update_ResP, Update_ResP2;

/*  HPF output filters for synthesis */
FILTER	OutputHPFZ, OutputHPFP;
/*  Filter coefficients for 2nd order Butterworth 275 Hz HPF:		*/
float	OutputHPFCoefsZ[3] = {0.946, -1.892, 0.946};
float 	OutputHPFCoefsP[3] = {1.0, -1.889033, 0.8948743};

/*

*************************************************************************
*                                                                         *
* ROUTINE
*		HPF_Speech
*
* FUNCTION
*		High pass filter input speech
* SYNOPSIS
*		HPF_Speech(speech_in)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	speech_in	float	 i/o	Frame of input speech
*
**************************************************************************/

void HPF_InSpeech(
float	speech[F_LEN])
{

	do_zfilt(&InputHPFZ, speech);
	do_pfilt(&InputHPFP, speech);

}


/*

*************************************************************************
*                                                                         *
* ROUTINE
*		HPF_OutSpeech
*
* FUNCTION
*		High pass filter output speech
* SYNOPSIS
*		HPF_Outspeech(speech_in, speech_out)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	speech_in	float	  i	Frame of input speech
*	speech_in	float	  o	Frame of output speech
*
**************************************************************************/

void HPF_OutSpeech(
float	speech_in[SF_LEN],
float	speech_out[SF_LEN])
{

/*  Perform filtering on input speech to produce output speech */
	MoveArray(SF_LEN, speech_in, speech_out);
	do_zfilt(&OutputHPFZ, speech_out);
	do_pfilt(&OutputHPFP, speech_out);

}


/*

*************************************************************************
*                                                                         *
* ROUTINE
*		FilterImpulseResponse
*
* FUNCTION
*		Filter Impulse Response with varying coefficients and
		  no history from past frames
* SYNOPSIS
*		FilterImpulseResponse(h, coef)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	h		float	 i/o	Impulse response
*	coef		float	  i	Filter coefficients
*
**************************************************************************/

void FilterImpulseResponse(
float	h[SF_LEN],
float	coef[ORDER+1])
{
int	t, j;
float	FiltMem[ORDER+1];

/*  Initialize memory */
	SetArray(ORDER+1, 0.0, FiltMem);
	
/*  Filter impulse response */
	for(t=0; t<SF_LEN; t++)	{
	  FiltMem[0] = h[t];
	  for(j=ORDER; j>0; j--)	{
	    FiltMem[0] -= coef[j] * FiltMem[j];
	    FiltMem[j]  = FiltMem[j-1];
	  }
	  h[t] = FiltMem[0];
	}
}
